package lol.clann.bukkit.pluginboot.model.gui;

import lol.clann.clannboot.annotation.inject.Inject;
import lol.clann.clannboot.context.BeanContext;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.Inventory;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

/**
 * @author pyz
 * @date 2018/8/12 下午3:08
 */
public class InventoryWrapper {

    private static final AtomicInteger atomicInteger = new AtomicInteger();

    private final Inventory inventory;
    private Consumer<InventoryClickEvent> click;
    private Consumer<InventoryOpenEvent> open;
    private Consumer<InventoryCloseEvent> close;
    private Consumer<InventoryCreativeEvent> creative;
    private Consumer<InventoryDragEvent> drag;
    private Consumer<InventoryPickupItemEvent> pickupItem;
    private int id;

    @Inject
    private InventoryListener inventoryListener;

    public InventoryWrapper(Inventory inventory) {
        BeanContext.inject(this);
        id = atomicInteger.incrementAndGet();
        this.inventory = inventory;
    }

    public Inventory getInventory() {
        return inventory;
    }

    public Consumer<InventoryClickEvent> getClick() {
        return click;
    }

    public InventoryWrapper setClick(Consumer<InventoryClickEvent> click) {
        this.click = click;
        return this;
    }

    public Consumer<InventoryOpenEvent> getOpen() {
        return open;
    }

    public InventoryWrapper setOpen(Consumer<InventoryOpenEvent> open) {
        this.open = open;
        return this;
    }

    public Consumer<InventoryCloseEvent> getClose() {
        return close;
    }

    public InventoryWrapper setClose(Consumer<InventoryCloseEvent> close) {
        this.close = close;
        return this;
    }

    public Consumer<InventoryCreativeEvent> getCreative() {
        return creative;
    }

    public InventoryWrapper setCreative(Consumer<InventoryCreativeEvent> creative) {
        this.creative = creative;
        return this;
    }

    public Consumer<InventoryDragEvent> getDrag() {
        return drag;
    }

    public InventoryWrapper setDrag(Consumer<InventoryDragEvent> drag) {
        this.drag = drag;
        return this;
    }

    public Consumer<InventoryPickupItemEvent> getPickupItem() {
        return pickupItem;
    }

    public InventoryWrapper setPickupItem(Consumer<InventoryPickupItemEvent> pickupItem) {
        this.pickupItem = pickupItem;
        return this;
    }

    public InventoryWrapper build() {
        inventoryListener.register(id, this);
        return this;
    }

    public void clear() {
        inventory.clear();
    }

    public void desroty() {
        clear();
        closeAll();
        inventoryListener.unregister(id);
    }

    public void closeAll() {
        //关闭所有gui视图
        HumanEntity[] views = inventory.getViewers().toArray(new HumanEntity[0]);
        for (HumanEntity view : views) {
            view.closeInventory();
        }
    }
}
